home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / recover / recoverUndo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.2 KB  |  191 lines

  1. /*
  2.  *   $RCSfile: recoverUndo.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:55 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "pool.h"
  45. #include "tid.h"
  46. #include "io.h"
  47. #include "lock.h"
  48. #include "object.h"
  49. #include "msgdefs.h"
  50. #include "thread.h"
  51. #include "semaphore.h"
  52. #include "link.h"
  53. #include "lsn.h"
  54. #include "latch.h"
  55. #include "bf.h"
  56. #include "volume.h"
  57. #include "trans.h"
  58. #include "openlog.h"
  59. #include "logrecs.h"
  60. #include "threadstate.h"
  61. #include "trans_extfuncs.h"
  62. #include "trans_intfuncs.h"
  63. #include "recover_intfuncs.h"
  64. #include "lm_extfuncs.h"
  65. #include "log_extfuncs.h"
  66. #include "thread_funcs.h"
  67. #include "thread_globals.h"
  68. #include "trans_globals.h"
  69. #include "recover_globals.h"
  70.  
  71.  
  72.  void
  73. recoverUndo (
  74.  
  75.     LSNOFFSET        lastLSN 
  76. )
  77. {
  78.  
  79.     TRANSREC        *transRec;        /* current transaction            */
  80.     TRANSREC        *nextTransRec;    /* next transaction to undo        */
  81.     int                maxFork;        /* max forked undo threads        */
  82.  
  83.  
  84.     TRACE(TR_TRANS, TR_LEVEL_1);
  85.  
  86.     initializeList(&(RecoverUndoWaitList));
  87.  
  88.     /* sort the transaction list by lastLSN */    
  89.     sortUndoList(&ActiveTransList, lastLSN);
  90.  
  91.     /*
  92.      *    At most fork only half of the available threads
  93.      */
  94.     maxFork = (NumThreads-CONST_THREADS)/2;
  95.     if (maxFork < 1) {
  96.         fprintf(sm_ErrorStream, "SERVER ERROR: insufficient threads to perform undo phase\n");
  97.         SM_ERROR(TYPE_STOP, esmNOFREETHREAD);
  98.     }
  99.  
  100.     /* search down the list */
  101.     UndoForkCount = 0;
  102.     nextTransRec = (TRANSREC *) FIRST_LIST_ELEMENT( &ActiveTransList );
  103.     while (nextTransRec != NULL)    {
  104.  
  105.         /* get the transaction with the largest nextUndoLSN */
  106.         transRec = nextTransRec;
  107.         nextTransRec = (TRANSREC *) NEXT_LIST_ELEMENT( &(nextTransRec->activeTransList) );
  108.  
  109.         /*
  110.          *  check the entry magic number
  111.          */
  112.         CHECK_TRANSREC_MAGIC(transRec);
  113.  
  114.         /*
  115.          *    if the transaction is a distributed transaction then
  116.          *    it cannot be thrown away because it must be past the
  117.          *    prepared state
  118.          */
  119.         if (LIST_MEMBER( &(transRec->distrTransList)))
  120.             continue;
  121.  
  122.         /*
  123.          *    If the transaction state is active, then abort it, otherwise
  124.          *  just remove it from the active list.
  125.          */
  126.         switch (transRec->transState)  {
  127.         case T_RECOVER:
  128.             /*
  129.              *  Record that the transaction is being aborted, but the
  130.              *  transaction is one that was left running at the time
  131.              *  of the crash, so its state is T_RECOVER.  If it is
  132.              *  set to T_ABORT then it looks like it is a transaction
  133.              *  started after the system restarted.
  134.              */
  135.             transRec->transState  = T_RECOVER;
  136.             transRec->intent      = T_ABORT;
  137.  
  138.             /* fork a thread to undo the transaction */
  139.             SM_ASSERT(LEVEL_3, sizeof(FOUR) == sizeof(transRec));
  140.             threadFork(T_PARENT, (PFI) recoverUndoTrans, 1, (FOUR*) &(transRec));
  141.             UndoForkCount++;
  142.  
  143.             /*
  144.              *    If we have forked too many threads, wait until one
  145.              *    currently active undo completes.
  146.              */
  147.             if (UndoForkCount == maxFork) {
  148.                 if (waitList(&RecoverUndoWaitList, THREAD_RECOVER_UNDO_WAIT)) {
  149.                     SM_ERROR(TYPE_CRASH, esmINTERNAL);
  150.                 }
  151.                 SM_ASSERT(LEVEL_3, UndoForkCount < maxFork);
  152.             }
  153.  
  154.             break;
  155.         case T_COMMIT:
  156.  
  157.             /* free the structures */
  158.             freeTransVolRecs(transRec);
  159.  
  160.             /* give back the transaction record */
  161.             freeTransRec(transRec);
  162.  
  163.             break;
  164.  
  165.         default:
  166. #ifdef DEBUG
  167.             TRPRINT( TR_RECOVER, TR_LEVEL_1, 
  168.             ("UNEXPECTED TRANS STATE: 0x%x for transaction # %d",  
  169.                 transRec->transState, transRec->tid));
  170.             SM_ERROR(TYPE_FATAL, esmINTERNAL);
  171.                 continue;
  172. #else
  173.             SM_ERROR(TYPE_FATAL, esmINTERNAL);
  174. #endif DEBUG
  175.         }
  176.     }
  177.  
  178.     /* wait for all undos to complete */
  179.     while (UndoForkCount > 0) {
  180.         if (waitList(&RecoverUndoWaitList, THREAD_RECOVER_UNDO_WAIT)) {
  181.             SM_ERROR(TYPE_CRASH, esmINTERNAL);
  182.         }
  183.     }
  184.     SM_ASSERT(LEVEL_3, UndoForkCount == 0);
  185.  
  186.     /*
  187.      *    no longer true because of the existence of distr trans
  188.      *    SM_ASSERT(LEVEL_1, LIST_EMPTY( &(ActiveTransList) ));
  189.      */
  190. }
  191.